package com.woniuxy.yogaapp.service.imp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.woniuxy.yogaapp.dto.IndexMes;
import com.woniuxy.yogaapp.mapper.CoachMapper;
import com.woniuxy.yogaapp.mapper.IndexMapper;
import com.woniuxy.yogaapp.mapper.OrderMapper;
import com.woniuxy.yogaapp.mapper.YogaClubMapper;
import com.woniuxy.yogaapp.pojo.ClubImage;
import com.woniuxy.yogaapp.pojo.Coach;
import com.woniuxy.yogaapp.pojo.Indexmes;
import com.woniuxy.yogaapp.pojo.YogaClub;
import com.woniuxy.yogaapp.service.IndexmesService;

import lombok.Data;

@Data
@Service
@Transactional
public class IndexmesServiceImp implements IndexmesService {

	@Resource
	private IndexMapper indexMapper;

	@Resource
	private CoachMapper coachMapper;

	@Resource
	private YogaClubMapper yogaClubMapper;

	// 展示所有已推送消息
	@Override
	public List<IndexMes> findIndexMes() {
		// Map用来存放dto对象
		List<IndexMes> indexmap = new ArrayList<IndexMes>();
		// 查询所有的推送消息
		List<Indexmes> indexmes = indexMapper.findstateIndexmes();
		// 循环遍历所有的推送消息
		for (int i = 0; i < indexmes.size(); i++) {
			int cid = indexmes.get(i).getCid();
			int yid = indexmes.get(i).getYid();
			int mesid = indexmes.get(i).getMesid();
			String state = indexmes.get(i).getState();
			String describe = indexmes.get(i).getDescribe();
			// 查询cid、yid对应的教练、场馆对象
			Coach coach = coachMapper.findCoachbyCid(cid);
			YogaClub club = yogaClubMapper.findClubbyYid(yid);
			List<ClubImage> ci = yogaClubMapper.findImagebyYid(yid);
			if (coach != null && club != null && ci != null) {
				IndexMes im = new IndexMes();
				im.setCdescribe(coach.getDescribe());
				im.setCgenre(coach.getCgenre());
				im.setCimage(coach.getCimage());
				im.setCname(coach.getCname());
				im.setYaddrmes(club.getYaddrmes());
				im.setYdescribe(club.getDescribe());
				im.setYname(club.getYname());
				im.setYtel(club.getYtel());
				im.setImage(ci);
				im.setState(state);
				im.setYid(yid);
				im.setCid(cid);
				im.setMesid(mesid);
				im.setDescribe(describe);
				indexmap.add(im);
			}
		}
		return indexmap;
	}

	// 展示所有推送消息至管理员界面
	@Override
	public List<IndexMes> findAllIndexMes() {
		// Map用来存放dto对象
		List<IndexMes> indexmap = new ArrayList<IndexMes>();
		// 查询所有的推送消息
		List<Indexmes> indexmes = indexMapper.findAllIndexmes();
		// 循环遍历所有的推送消息
		for (int i = 0; i < indexmes.size(); i++) {
			int cid = indexmes.get(i).getCid();
			int yid = indexmes.get(i).getYid();
			int mesid = indexmes.get(i).getMesid();
			String state = indexmes.get(i).getState();
			String describe = indexmes.get(i).getDescribe();
			// 查询cid、yid对应的教练、场馆对象
			Coach coach = coachMapper.findCoachbyCid(cid);
			YogaClub club = yogaClubMapper.findClubbyYid(yid);
			List<ClubImage> ci = yogaClubMapper.findImagebyYid(yid);
			if (coach != null && club != null && ci != null) {
				IndexMes im = new IndexMes();
				im.setCdescribe(coach.getDescribe());
				im.setCgenre(coach.getCgenre());
				im.setCimage(coach.getCimage());
				im.setCname(coach.getCname());
				im.setYaddrmes(club.getYaddrmes());
				im.setYdescribe(club.getDescribe());
				im.setYname(club.getYname());
				im.setYtel(club.getYtel());
				im.setImage(ci);
				im.setState(state);
				im.setYid(yid);
				im.setCid(cid);
				im.setMesid(mesid);
				im.setDescribe(describe);
				indexmap.add(im);
			}
		}
		return indexmap;
	}

	// 新增推送消息
	@Override
	public int addIndexmes(Indexmes indexmes) {
		int yid = indexmes.getYid();
		int cid = indexmes.getCid();
		String describe = indexmes.getDescribe();
		int row = indexMapper.addIndexmes(yid, cid, describe);
		return row;
	}

	// 删除推送消息
	@Override
	public int deleteIndexmes(Indexmes indexmes) {
		int mesid = indexmes.getMesid();
		int row = indexMapper.deleteIndexmes(mesid);
		return row;
	}

	// 推送消息至主页
	@Override
	public int sendIndexmes(Indexmes indexmes) {
		int mesid = indexmes.getMesid();
		int row = indexMapper.changeMestoIndex(mesid);
		return row;
	}

	// 不再推送消息
	@Override
	public int notSendMes(Indexmes indexmes) {
		int mesid = indexmes.getMesid();
		int row = indexMapper.changeMesagain(mesid);
		return row;
	}

}
